﻿<%@ Page Language="C#" MasterPageFile="~/Popup.Master" AutoEventWireup="true" CodeBehind="DocHeader.aspx.cs"
    Inherits="PromotionEngine.DocHeader" Title="Promotion Engine -  Promotion" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <script type="text/javascript">
        var oCompany = {};
        var isInsert = false;
        var companyData;
        var gird;
        var fileUpload;
        var keySearch = '', keyWords = '';
        function Search() {
            keySearch = $("#KeySearch").val();
            keyWords = $("#KeyWords").val();
            grid.dataSource.read();
        }
        function addNewDetails() {
            var wnd = $("#details")
                        .kendoWindow({
                            title: "Document Detail",
                            modal: true,
                            visible: false,
                            resizable: false,
                            width: 1200,
                            height: 600,
                            content: { type: "POST", url: "DocLine.aspx?DocEntry=0&IsUpdate=false" },
                            iframe: true
                        }).data("kendoWindow");

            wnd.center().open();
            wnd.bind('close', function () {
                grid.dataSource.read();
                $.ajax({
                    type: "POST",
                    url: "DocLine.aspx/ClearSessionData",
                    data: '',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                    },
                    failure: function (response) {
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        if (jqXHR.status == 500) {
                            alert('Internal error: ' + jqXHR.responseText);
                        } else {
                            alert('Unexpected error.' + jqXHR.responseText);
                        }
                    }
                });
            })
        }
        function showDetails(e) {
            e.preventDefault();
            var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
            var detailUrl = "DocLine.aspx?DocEntry=" + dataItem.DocEntry + "&DocDate=" + dataItem.DocDate
                        + "&DocDueDate=" + dataItem.DocDueDate + "&CustomerID=" + dataItem.CustomerID
                        + "&Remark=" + dataItem.Remark + "&IsUpdate=true"
            var wnd = $("#details")
                        .kendoWindow({
                            title: "Document Detail",
                            modal: true,
                            visible: false,
                            resizable: false,
                            width: 1200,
                            height: 600,
                            content: { type: "POST", url: detailUrl, data: { "header": kendo.stringify(dataItem)} },
                            iframe: true
                        }).data("kendoWindow");

            wnd.center().open();
            wnd.bind('close', function () {
                $("#details").empty();
                grid.dataSource.read();
                $.ajax({
                    type: "POST",
                    url: "DocLine.aspx/ClearSessionData",
                    data: '',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                    },
                    failure: function (response) {
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        if (jqXHR.status == 500) {
                            alert('Internal error: ' + jqXHR.responseText);
                        } else {
                            alert('Unexpected error.' + jqXHR.responseText);
                        }
                    }
                });
            })
        }
        function showLink(e) {
            e.preventDefault();
            var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
            var wnd = $("#link")
                        .kendoWindow({
                            title: "Doc Property Link",
                            modal: true,
                            visible: false,
                            resizable: false,
                            width: 705,
                            height: 550,
                            content: { type: "POST", url: "Doc-Property-Link.aspx?DocEntry=" + dataItem.DocEntry + "", data: { "customer": kendo.stringify(dataItem)} },
                            iframe: true
                        }).data("kendoWindow");

            wnd.center().open();
        }
        function onChange(arg) {
            var selectedRows = this.select();
            var selectedDataItems = [];
            for (var i = 0; i < selectedRows.length; i++) {
                var dataItem = this.dataItem(selectedRows[i]);
                selectedDataItems.push(dataItem);
            }
            if (selectedDataItems.length > 0) {
                parent.LoadDocument(selectedDataItems[0].DocEntry, selectedDataItems[0].DocDate, selectedDataItems[0].DocDueDate,
                                     selectedDataItems[0].Remark, selectedDataItems[0].CustomerID, 'true', selectedDataItems[0].DocStatus, selectedDataItems[0].ReferenceName, selectedDataItems[0].CustomerName);
                parent.$("#documentList").data("kendoWindow").close();
            }
        }
        $(document).ready(function () {
            $("#KeySearch").keydown(function (event) {
                if (event.keyCode == 13) {
                    event.preventDefault();
                    Search();
                    return false;
                }
            });
            $("#KeyWords").keydown(function (event) {
                if (event.keyCode == 13) {
                    event.preventDefault();
                    Search();
                    return false;
                }
            });
            setTimeout(function () { $("#KeySearch").focus(); }, 0);
            keySearch = $("#KeySearch").val();
            keyWords = $("#KeyWords").val();

            grid = $("#grid").kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            type: "GET",
                            url: "DocHeader.aspx/GetDocHeadersForSearch",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json"
                        },
                        destroy: {
                            type: "GET",
                            url: "DocHeader.aspx/DeleteDocument",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json"
                        },
                        parameterMap: function (data, operation) {
                            if (operation === "destroy") {
                                return { docEntry: data.DocEntry };
                            }
                            if (operation === "read") {
                                return {
                                    keySearch: kendo.stringify(keySearch),
                                    keyWords: kendo.stringify(keyWords)
                                };
                            }
                        },
                        serverPaging: true,
                        serverSorting: true,
                        serverFiltering: true
                    },
                    pageSize: 20,
                    type: "odata", //Important!!!!
                    schema: {
                        data: function (response) {
                            if (response.d != null && response.d != '') {
                                return JSON.parse(response.d);
                            }
                        },
                        total: function (response) {
                            if (response.d != '') {
                                return JSON.parse(response.d).length;
                            }
                            else {
                                return 0;
                            }
                        },
                        model: {
                            id: "DocEntry",
                            fields: {
                                DocEntry: { type: "string", validation: { required: true} },
                                CompanyCode: { type: "string", validation: { required: true} },
                                CompanyName: { type: "string", validation: { required: true} },
                                CustomerID: { type: "string", validation: { required: true} },
                                CustomerCode: { type: "string", validation: { required: true} },
                                CustomerName: { type: "string", validation: { required: true} },
                                DocDate: { type: "Date", validation: { required: true} },
                                DocTime: { type: "Date", validation: { required: true} },
                                DocDueDate: { type: "Date", validation: { required: true} },
                                SubTotal: { type: "number", validation: { required: true} },
                                DocStatus: { type: "number", validation: { required: true} },
                                ReferenceName: { type: "string", validation: { required: true} },
                                Remark: { type: "string", validation: { required: true} }
                            }
                        }
                    }
                },
                change: onChange,
                sortable: true,
                pageable: true,
                pageable: {
                    pageSize: 20,
                    refresh: true
                },
                groupable: true,
                filterable: true,
                reorderable: true,
                resizable: true,
                selectable: "row",
                height: 430,
                toolbar: [ { name: 'excel'}],
                excel: {
                    fileName: "Document.xlsx",
                    filterable: true,
                    allPages: true
                },
                batch: true,
                columns: [{ field: "DocEntry", title: "Doc Entry", width: 120 },
                            { field: "CompanyCode", title: "Company Code", width: 200, hidden: true },
                            { field: "CompanyName", title: "Company Name", width: 160, hidden: true },
                            { field: "CustomerCode", title: "Customer Code", width: 150 },
                            { field: "CustomerID", title: "Customer ID", width: 300, hidden: true },
                            { field: "CustomerName", title: "Customer Name", width: 200 },
                            { field: "DocDate", title: "DocDate", template: '#= kendo.toString(DocDate, "dd/MM/yyyy") #' },
                            { field: "DocTime", title: "DocTime", template: '#= kendo.toString(DocTime, "hh:mm:tt") #', hidden: true },
                            { field: "DocDueDate", title: "DocDueDate", template: '#= kendo.toString(DocDueDate, "dd/MM/yyyy") #', hidden: true },
                            { field: "SubTotal", title: "SubTotal", format: "{0:n}", type: "number", attributes: { style: "text-align:right;"} },
                            { field: "DocStatus", title: "DocStatus", width: 300, hidden: true },
                            { field: "ReferenceName", title: "Status", width: 100 },
                            { field: "Remark", title: "Remark", width: 300 },
                            { command: ["destroy"], title: "Action", width: "100px" }
                            ],
                editable: {
                    mode: "popup",
                    confirmation: "Are you sure you want to remove this document?"
                },
                edit: function (e) {
                    var editWindow = e.container.data("kendoWindow");
                    if (e.model.isNew()) {
                        e.container.data("kendoWindow").title('Add New Company');
                        $("a.k-grid-update")[0].innerHTML = "<span class='k-icon k-update'></span>Save";
                        isInsert = true;
                    }
                    else {
                        e.container.data("kendoWindow").title('Edit Company');
                        $('[name="CompanyCode"]').attr("readonly", true);
                        isInsert = false;
                    }
                }
            }).data("kendoGrid");
         
        });
    </script>
    <script id="grid_toolbar" type="text/x-kendo-template">
   <a href="\#" class="k-button k-button-icontext" id="grid_toolbar_queryBtn"       
            onclick="return addNewDetails();"><span class="k-icon k-i-excel"></span>Add New Document</a>
    </script>
    <div id="details">
    </div>
    <div id="link">
    </div>
    <table style="width: 100%;" border="0">
        <tr>
            <td>
                Doc Entry
            </td>
            <td style="width: 20%;">
                <input type="text" class="k-input k-textbox" name="KeySearch" id="KeySearch" style="width: 90%;"  placeholder="key search" />
            </td>
            <td>
               Search
            </td>
            <td style="width: 55%;">
                <input type="text" class="k-input k-textbox" name="KeyWords" id="KeyWords" style="width: 100%;"   placeholder="enter customer code or customer name"/>
            </td>
            <td align="right">
                <button type="button" id="btnSave" onclick="Search();" class="k-button k-button-icontext k-primary"
                    style="width: 150px;">
                    <span class="k-icon k-i-search"></span>Find</button>
            </td>
        </tr>
    </table>
    <div style="position: absolute; z-index: 0; margin-right: 8px;margin-top:8px;">
        <div id="grid">
        </div>
    </div>
</asp:Content>
